package com.nulldev.util.web.YouTubeExtractor.related;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.web.YouTubeExtractor.util.YouTubeAmountFormatter;

public class VideoInfo {
	private static final Logger log = LoggerFactory.getLoggerD(Arguments.hasArgument("--VideoInfo.debug"));

	public static enum VideoInfoType {
		VIDEO("video"),
		PLAYLIST("playlist"),
		SHOW("show")

		;

		private final String type;

		private VideoInfoType(final String type) {
			this.type = type;
		}

		public String getType() {
			return this.type;
		}
	}

	private final String name;
	private final String link;
	private final String time;
	private String user;
	private final String views;
	private long views_parsed = -1;
	private long duration;
	private String thumbnailID;
	private boolean isPlaylist;
	private boolean isUpnext;
	private String animatedThumbnailURL;

	public VideoInfo(String name, String link, String user, String time, String views) {
		this.name = name;
		this.link = link;
		this.user = user;
		this.time = time;
		this.views = views;
		this.thumbnailID = link;
		if (this.views != null) {
			try {
				log.debug("VIDEO_INFO_INPUT_STRING: " + this.views);
				this.views_parsed = YouTubeAmountFormatter.format(this.views);
			} catch (Exception e) {
				log.error("Failed to parse views!", e);
			}
		}
	}

	public String getName() {
		return this.name;
	}

	public String getID() {
		return this.link;
	}

	public String getOnlyID() {
		if (!this.link.contains("&list")) {
			return this.link;
		} else {
			return this.link.substring(0, link.indexOf('&'));
		}
	}

	public String getViews() {
		return this.views;
	}

	public long getViewCount() {
		return this.views_parsed;
	}

	public boolean hasViewCount() {
		return this.views_parsed != -1;
	}

	public long getDuration() {
		return this.duration;
	}

	public VideoInfo setDuration(final long duration) {
		this.duration = duration;
		return this;
	}

	public String getThumbnailID() {
		return this.thumbnailID;
	}

	protected VideoInfo setThumbnailID(final String id) {
		this.thumbnailID = id;
		return this;
	}

	public boolean isPlaylist() {
		return this.isPlaylist;
	}

	protected VideoInfo setPlaylist(final boolean value) {
		this.isPlaylist = value;
		return this;
	}

	public boolean isUpnext() {
		return this.isUpnext;
	}

	protected VideoInfo setUpnext(final boolean value) {
		this.isUpnext = value;
		return this;
	}

	public String getUser() {
		return user;
	}

	public String getTime() {
		return time;
	}

	@Override
	public String toString() {
		return "VideoInfo[name=" + this.name + ",id=" + this.link + ",views=" + this.views + ",user=" + this.user + ",time=" + this.time + "]";
	}

	public void ___setUser(final String author) {
		this.user = author;
	}

	public VideoInfo setAnimatedURL(final String animatedThumbnail) {
		this.animatedThumbnailURL = animatedThumbnail;
		return this;
	}

	public boolean hasAnimatedThumbnail() {
		return this.animatedThumbnailURL != null;
	}

	public String getAnimatedThumbnail() {
		return this.animatedThumbnailURL;
	}

	private VideoInfoType type = VideoInfoType.VIDEO;

	public VideoInfoType getType() {
		return this.type;
	}

	protected VideoInfo setType(final VideoInfoType type) {
		if (type == null)
			return this;
		this.type = type;
		return this;
	}

}
